package com.funsport.util;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * 密码哈希生成器
 * 用于生成BCrypt密码哈希
 * 
 * 运行这个类来生成admin123456的正确哈希值
 */
public class PasswordHashGenerator {
    
    public static void main(String[] args) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        
        // 生成多个常用密码的哈希
        System.out.println("=============================================");
        System.out.println("密码哈希生成器");
        System.out.println("=============================================\n");
        
        // 密码1: admin
        generatePasswordHash(encoder, "admin");
        
        System.out.println("\n---------------------------------------------\n");
        
        // 密码2: admin123456
        generatePasswordHash(encoder, "admin123456");
        
        System.out.println("\n=============================================");
        System.out.println("注意：每次运行生成的哈希都不同，但都能验证同一个密码");
        System.out.println("这是BCrypt的安全特性（salt随机生成）");
        System.out.println("=============================================");
    }
    
    private static void generatePasswordHash(BCryptPasswordEncoder encoder, String password) {
        // 生成哈希
        String hash = encoder.encode(password);
        
        System.out.println("【密码: " + password + "】");
        System.out.println("BCrypt哈希: " + hash);
        System.out.println();
        System.out.println("SQL更新语句（直接复制使用）:");
        System.out.println("UPDATE users SET password_hash = '" + hash + "' WHERE nickname = 'admin';");
        System.out.println();
        
        // 验证哈希是否正确
        boolean matches = encoder.matches(password, hash);
        System.out.println("验证密码: " + (matches ? "✅ 成功" : "❌ 失败"));
        
        // 测试其他密码
        System.out.println("\n测试验证:");
        testPassword(encoder, hash, password, "✅ 正确密码 [" + password + "]");
        testPassword(encoder, hash, "wrongpassword", "❌ 错误密码 [wrongpassword]");
    }
    
    private static void testPassword(BCryptPasswordEncoder encoder, String hash, String testPassword, String description) {
        boolean matches = encoder.matches(testPassword, hash);
        System.out.println(String.format("%-20s: %s", description, matches ? "✅" : "❌"));
    }
}

